package com.eascs.ticket.ticket.controller;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.assertion.util.AssertInfoUtils;
import com.eascs.common.util.ObjectUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.ticket.entity.TicketAssociation;
import com.eascs.ticket.ticket.form.GetEditViewForm;
import com.eascs.ticket.ticket.form.TicketAssociationForm;
import com.eascs.ticket.ticket.service.TicketAssociationService;

@RestController
public class TicketAssociationRestController extends BaseRestController {

	@Inject
	private TicketAssociationService ticketAssociationService;

	@RequestMapping(value = "/ticket/opt/getAssociationEditView")
	public HtmlRestResult getAssociationEditView(GetEditViewForm getEditViewForm) {
		TicketAssociation ticketAssociation = null;
		if (StringUtil.isEmpty(getEditViewForm.getId())) {
			ticketAssociation = new TicketAssociation();
			ticketAssociation.setTicketId(getEditViewForm.getTicketId());
		} else {
			ticketAssociation = ticketAssociationService.getTicketAssociationById(getEditViewForm.getId());
		}
		ViewContext.put("ticketAssociation", ticketAssociation);
		return HtmlRestResult.popHtmlFragment("/ticket/opt/getAssociationEditView");
	}

	@RequestMapping(value = "/ticket/opt/saveTicketAssociation")
	public BaseRestResult<String> saveTicketAssociation(TicketAssociationForm ticketAssociationForm) {
		AssertInfoUtils.assertTrue(!ticketAssociationForm.getAssociationTicketId().equals(ticketAssociationForm.getTicketId()), "关联issue必须不同");
		TicketAssociation ticketAssociation = new TicketAssociation();
		ObjectUtil.copySimpleProperties(ticketAssociationForm, ticketAssociation);
		EntityUtil.initInsertEntity(ticketAssociation);
		this.ticketAssociationService.insertTicketAssociation(ticketAssociation);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/ticket/opt/deleteTicketAssociation")
	public BaseRestResult<String> deleteTicketAssociation(TicketAssociationForm ticketAssociationForm) {
		TicketAssociation ticketAssociation = new TicketAssociation();
		ticketAssociation.setId(ticketAssociationForm.getId());
		this.ticketAssociationService.deleteTicketAssociation(ticketAssociation);
		return BaseRestResult.refresh();
	}

}
